package tour.spring.web.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import tour.spring.web.commandobject.RegisterUserForm;

public class RegisterUserValidator implements Validator {


		@Override
		public boolean supports(Class clazz) {
			return RegisterUserForm.class.equals(clazz);
		}

		@Override
		public void validate(Object obj, Errors errors) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "registerUserForm.login.empty");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "registerUserForm.password.empty");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "registerUserForm.name.empty");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "registerUserForm.surname.empty");

			RegisterUserForm user = (RegisterUserForm)obj;

			if (user.getLogin()!=null && user.getPassword()!=null)
				if ( user.getLogin().equalsIgnoreCase(user.getPassword()))
					errors.rejectValue( "password", "registerUserForm.password.sameloginpasswd");
		}
	}

//login;
//private String password;
//private String name;
//private String surname;